#
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
cmake_minimum_required(VERSION 3.4.1)
include(ExternalProject)

# TENSORFLOW_ROOT_DIR:
#    root directory of tensorflow repo
#    used for shared source files and pre-built libs
get_filename_component(TENSORFLOW_ROOT_DIR ../../../.. ABSOLUTE)
set(PREBUILT_DIR ${TENSORFLOW_ROOT_DIR}/tensorflow/contrib/makefile/gen)

add_library(lib_proto STATIC IMPORTED )
set_target_properties(lib_proto PROPERTIES IMPORTED_LOCATION
    ${PREBUILT_DIR}/protobuf/lib/libprotobuf.a)

add_library(lib_tf STATIC IMPORTED )
set_target_properties(lib_tf PROPERTIES IMPORTED_LOCATION
        ${PREBUILT_DIR}/lib/libtensorflow-core.a)
# Change to compile flags should be replicated into bazel build file
# LINT.IfChange
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-rtti -fno-exceptions \
                     -fpic -O2 -mfpu=neon -DTF_LEAN_BINARY -msse4.1 \
                     -DGOOGLE_PROTOBUF_NO_RTTI \
                     -DGOOGLE_PROTOBUF_NO_STATIC_INITIALIZER")
# LINT.ThenChange(//tensorflow/tensorflow.bzl)

set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} \
                              -Wl,--allow-multiple-definition \
                              -Wl,--whole-archive")

file(GLOB tensorflow_inference_sources
     ${CMAKE_CURRENT_SOURCE_DIR}/../jni/*.cc)
add_library(tensorflow_inference SHARED ${tensorflow_inference_sources})

# Include libraries needed for hello-jni lib
target_link_libraries(tensorflow_inference
                      android
                      log
                      m
                      z
                      lib_tf
                      lib_proto)
include_directories(
    ${PREBUILT_DIR}/proto
    ${PREBUILT_DIR}/protobuf/include
    ${TENSORFLOW_ROOT_DIR}/tensorflow/contrib/makefile/downloads/eigen
    ${TENSORFLOW_ROOT_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/..)
